package com.mec.web.y2012.coreservlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

@WebServlet("/ShowRequestHeadersServlet")
public class ShowRequestHeadersServlet extends HttpServlet {

	private static final long serialVersionUID = 6844889030094328368L;


	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setContentType("text/html");
		PrintWriter out = resp.getWriter();
		
		//
		String title = "Servlet Example: Showing Request Headers";
		out.println(Constants.docType + ServletUtilities.headWithTitle(title) +
				"<body bgcolor=\"#fdf5e6\">\n<h1>" + title + "</h1><br/>" +
				"<strong>Request Method: </strong>" + req.getMethod() + "<br/>" +
				"<strong>Request URI:</strong>" + req.getRequestURI() + "<br/>" +
				"<strong>Request Protocol: </strong> " + req.getProtocol() + "<br/>" +
				"<table border = 1 ><tr><th>Header Name</th><th>Header Value</th></tr>" 
				);
		Enumeration<String> headers = req.getHeaderNames();
		while(headers.hasMoreElements()){
			String headerName = headers.nextElement();
			out.println("<tr><td>" + headerName + "</td><td>" + req.getHeader(headerName) + "</td></tr>");
		}
		out.println("</table></body></html>");
		
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}

	
	private static final Logger logger = 
			Logger.getLogger(ShowRequestHeadersServlet.class);
}
